package com.teamclub.app.sconfs.oauth

import java.security.MessageDigest
import java.util.UUID

object TokenGenerator {

  def main(args: Array[String]) = {
    println(generateValue())
  }

  private val hexCode = "0123456789abcdef".toCharArray

  def generateValue(): String = {
    generateValue(UUID.randomUUID.toString)
  }
  def generateValue(param: String) = {
    try {
      val algorithm = MessageDigest.getInstance("MD5")
      algorithm.reset()
      algorithm.update(param.getBytes())
      val messageDigest = algorithm.digest()
      toHexString(messageDigest)
    } catch {
      case e: Exception => {
        throw new RuntimeException("生成Token失败", e)
      }
    }
  }
  def toHexString(data: Array[Byte]): String = {
    if(data == null) {
      return null;
    }
    val r = new StringBuilder(data.length * 2)
    for(b: Byte <- data) {
      r.append(hexCode((b >> 4) & 0xF))
      r.append(hexCode(b & 0xF))
    }
    r.toString
  }
}